package httpHandle

import (
	"github.com/gin-gonic/gin"
	"github.com/wzh200x/webServer/constants"
	"github.com/wzh200x/webServer/httpHandle/service"
	"github.com/wzh200x/webServer/middleware/auth"
	"github.com/wzh200x/webServer/middleware/response"
)

// 使用service包中定义的请求和响应结构
type UpdateModuleRequest service.UpdateModuleRequest
type QueryModuleRequest service.QueryModuleRequest
type ModuleResponse service.ModuleResponse

// HandleUpdateModule 处理更新角色模块数据请求
func HandleUpdateModule(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)

	// 从上下文中获取用户信息
	userID, username := auth.GetCurrentUser(c)

	// 解析请求参数
	var req UpdateModuleRequest
	if bindErr := c.ShouldBind(&req); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}

	// 转换为service层的请求结构
	serviceReq := service.UpdateModuleRequest{
		AvatarId:           req.AvatarId,
		HyperNexusAvatarId: req.HyperNexusAvatarId,
		ModuleId:           req.ModuleId,
		Data:               req.Data,
	}

	// 调用service层更新模块
	module, errCode, errMsg := service.UpdateModule(userID, username, serviceReq)
	if errCode > 0 {
		err(errCode, errMsg)
		return
	}

	// 返回更新后的模块信息
	success(module)
}

// HandleQueryModule 处理查询角色模块数据请求
func HandleQueryModule(c *gin.Context) {
	success, err := response.GetResponseFuncs(c)

	// 从上下文中获取用户信息
	userID, username := auth.GetCurrentUser(c)

	// 解析请求参数
	var req QueryModuleRequest
	if bindErr := c.ShouldBind(&req); bindErr != nil {
		err(constants.ErrUnknown, "无效的请求参数")
		return
	}

	// 转换为service层的请求结构
	serviceReq := service.QueryModuleRequest{
		AvatarId:           req.AvatarId,
		HyperNexusAvatarId: req.HyperNexusAvatarId,
		ModuleId:           req.ModuleId,
	}

	// 调用service层查询模块
	module, errCode, errMsg := service.QueryModule(userID, username, serviceReq)
	if errCode > 0 {
		err(errCode, errMsg)
		return
	}

	// 返回模块信息
	success(module)
}
